﻿using Nacos.AspNetCore.V2;
using Nacos.V2.DependencyInjection;

namespace MyWeb.ScheduledTasks.Nacos
{
    /// <summary>
    /// Nacos 启动服务
    /// 安装 NuGet包：nacos-sdk-csharp
    /// </summary>
    public static class NacosSetup
    {
        public static void AddNacosSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            // 注册Nacos配置中心
            services.AddNacosV2Config(x =>
            {
                x.ServerAddresses = new System.Collections.Generic.List<string> { "http://172.18.18.250:8848/" };
                x.EndPoint = "";
                x.Namespace = "";

                /*x.UserName = "nacos";
               x.Password = "nacos";*/

                // swich to use http or rpc
                x.ConfigUseRpc = true;//是否使用最新的配置  ConfigUseRpc 和 NamingUseRpc，分别表示配置和服务是否要使用最新的 gRPC 协议和服务端对接。
            });
            // 注册Nacos服务发现
            services.AddNacosV2Naming(x =>
            {
                x.ServerAddresses = new System.Collections.Generic.List<string> { "http://172.18.18.250:8848/" };
                x.EndPoint = "";
                x.Namespace = "";

                /*x.UserName = "nacos";
               x.Password = "nacos";*/

                // swich to use http or rpc
                x.NamingUseRpc = true;//是否使用最新的服务
            });

            // 注册服务到Nacos
            services.AddNacosAspNet(x =>
            {
                x.ServerAddresses = new System.Collections.Generic.List<string> { "http://172.18.18.250:8849/" };
                x.Namespace = "";
                //x.UserName = "nacos";
                //x.Password = "nacos";
                x.GroupName = "NET";
                x.ServiceName = "MyWeb.ScheduledTasks";
                x.Weight = 1;//设置权重
            });
        }
    }
}
